package cn.jiyun.service.impl;

import cn.jiyun.entity.XmDistributionstations;
import cn.jiyun.mapper.XmDistributionstationsMapper;
import cn.jiyun.service.IXmDistributionstationsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-26
 */
@Service
public class XmDistributionstationsServiceImpl extends ServiceImpl<XmDistributionstationsMapper, XmDistributionstations> implements IXmDistributionstationsService {


    @Override
    public Page<XmDistributionstations> findAll(Integer current, Integer pageSize, XmDistributionstations xmDistributionstations) {
        Page<XmDistributionstations> xmDistributionstationsPage = new Page<>();
        QueryWrapper<XmDistributionstations> xmDistributionstationsQueryWrapper = new QueryWrapper<>();
        if(xmDistributionstations.getId()!=null){
            xmDistributionstationsQueryWrapper.eq("id",xmDistributionstations.getId());
        }
        if(xmDistributionstations.getName()!=null && !xmDistributionstations.getName().equals("")){
            xmDistributionstationsQueryWrapper.like("name",xmDistributionstations.getName());
        }
       if(xmDistributionstations.getPname()!=null && !xmDistributionstations.getPname().equals("")){
           xmDistributionstationsQueryWrapper.like("pname",xmDistributionstations.getPname());
       }
       if(xmDistributionstations.getYname()!=null && !xmDistributionstations.getYname().equals("")){
           xmDistributionstationsQueryWrapper.like("yname",xmDistributionstations.getYname());
       }
       if(xmDistributionstations.getTel()!=null&& !"".equals(xmDistributionstations.getTel())){
           xmDistributionstationsQueryWrapper.like("tel",xmDistributionstations.getTel());
       }
       if(xmDistributionstations.getAddress()!=null&& !"".equals(xmDistributionstations.getAddress())){
           xmDistributionstationsQueryWrapper.like("address",xmDistributionstations.getAddress());
       }
        if ( xmDistributionstations.getZtime()!= null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if ( xmDistributionstations.getZtime() == 1) {
                xmDistributionstationsQueryWrapper.apply("ABS(DATEDIFF({0}, opening_time)) <= {1}", now, mo);
            }

            if( xmDistributionstations.getZtime()==2){
                xmDistributionstationsQueryWrapper.apply("YEAR(opening_time)=YEAR(CURRENT_DATE)");
            }

            if( xmDistributionstations.getZtime()==3){
                xmDistributionstationsQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2022");
            }

            if( xmDistributionstations.getZtime()==4){
                xmDistributionstationsQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2021");
            }
            if( xmDistributionstations.getZtime()==5){
                xmDistributionstationsQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2020");
            }
            if( xmDistributionstations.getZtime()==6){
                xmDistributionstationsQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')<2020");
            }
        }

        if(xmDistributionstations.getStatus()!=null){
            xmDistributionstationsQueryWrapper.eq("status",xmDistributionstations.getStatus());
        }
        return baseMapper.selectPage(xmDistributionstationsPage,xmDistributionstationsQueryWrapper);
    }
}
